#include "TerrainRender.h"

TileShadowMgr::TileShadowMgr() {
}

TileShadowMgr::~TileShadowMgr() {
}

void TileShadowMgr::RenderShadows( TILERENDERSPEC *tlist, UINT ntile ) {

	iCtx->IASetInputLayout( IL_TILEVERTEX_HIGH );
	iCtx->VSSetShader( VS_Shadow, NULL, 0 );
	iCtx->PSSetShader( PS_Shadow, NULL, 0 );
	iCtx->OMSetRenderTargets( 1, &TerrainShadowVolumeRTV, SC->RTarget_DSV );

	for( UINT j = 0; j < ntile; j++ ) {
		;
	}

	iCtx->OMSetRenderTargets( 1, &SC->RTarget_RTV, SC->RTarget_DSV );
}

//================================================
//				Static
//================================================

ID3D11InputLayout
	*TileShadowMgr::IL_TILEVERTEX_HIGH = NULL;
ID3D11VertexShader
	*TileShadowMgr::VS_Shadow = NULL;
ID3D11PixelShader
	*TileShadowMgr::PS_Shadow = NULL;
ID3D11RenderTargetView
	*TileShadowMgr::TerrainShadowVolumeRTV = NULL;
ID3D11ShaderResourceView
	*TileShadowMgr::TerrainShadowVolumeSRV = NULL;

void TileShadowMgr::GlobalInit() {

	//====================================================
	//			Shaders
	//====================================================

	D3D11_INPUT_ELEMENT_DESC ldesc_0[ ] = {
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 36, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	};
	
	HRESULT hr;
	ID3DBlob *SBlob = NULL, *EBlob = NULL;

	::CompileFromFile( "Modules\\D3D11Shaders\\Terrain\\VS_Shadow.fx", NULL, NULL, "VS_Shadow", vs_ver, ShaderCompileFlag, 0, NULL, &SBlob, &EBlob, &hr );
	ShowShaderCompilationError( hr, EBlob );
	Dev->CreateInputLayout( ldesc_0, 4, SBlob->GetBufferPointer(), SBlob->GetBufferSize(), &IL_TILEVERTEX_HIGH );
	Dev->CreateVertexShader( SBlob->GetBufferPointer(), SBlob->GetBufferSize(), NULL, &VS_Shadow );
	REL( SBlob );
	REL( EBlob );

	::CompileFromFile( "Modules\\D3D11Shaders\\Terrain\\PS_Shadow.fx", NULL, NULL, "PS_Shadow", vs_ver, ShaderCompileFlag, 0, NULL, &SBlob, &EBlob, &hr );
	ShowShaderCompilationError( hr, EBlob );
	Dev->CreatePixelShader( SBlob->GetBufferPointer(), SBlob->GetBufferSize(), NULL, &PS_Shadow );
	REL( SBlob );
	REL( EBlob );

	//====================================================
	//			Render target
	//====================================================

	ID3D11Texture2D *tex;
	D3D11_TEXTURE2D_DESC tdesc;
	D3D11_RENDER_TARGET_VIEW_DESC rtvdesc;
	D3D11_SHADER_RESOURCE_VIEW_DESC srvdesc;

	memset( &tdesc, 0, sizeof(tdesc) );
	tdesc.ArraySize = 1;
	tdesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
	tdesc.CPUAccessFlags = 0;
	tdesc.Format = DXGI_FORMAT_R32_FLOAT;
	tdesc.Height = cfg->cHeight;
	tdesc.Width = cfg->cWidth;
	tdesc.MipLevels = 1;
	tdesc.MiscFlags = 0;
	tdesc.SampleDesc.Count = 1;
	tdesc.Usage = D3D11_USAGE_DEFAULT;

	Dev->CreateTexture2D( &tdesc, NULL, &tex );

	memset( &rtvdesc, 0, sizeof(rtvdesc) );
	rtvdesc.Format = DXGI_FORMAT_R32_FLOAT;
	rtvdesc.Texture2D.MipSlice = 0;
	rtvdesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;

	Dev->CreateRenderTargetView( tex, &rtvdesc, &TerrainShadowVolumeRTV );

	memset( &srvdesc, 0, sizeof(srvdesc) );
	srvdesc.Format = DXGI_FORMAT_R32_FLOAT;
	srvdesc.Texture2D.MipLevels = 1;
	srvdesc.Texture2D.MostDetailedMip = 0;
	srvdesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;

	Dev->CreateShaderResourceView( tex, &srvdesc, &TerrainShadowVolumeSRV );

	//====================================================
	//
	//====================================================

}

void TileShadowMgr::GlobalExit() {
	REL( IL_TILEVERTEX_HIGH );
	REL( VS_Shadow );
	REL( PS_Shadow );
	REL( TerrainShadowVolumeRTV );
	REL( TerrainShadowVolumeSRV );
}